//
//  SimPickerView.m
//
//  Created by LiuXubin on 15/9/1.
//

#import "SimPickerView.h"
#import "UIView+SimAdditions.h"

SimImpValue(kSimPickerItemColor, UIColor *)
SimImpValue(kSimPickerContentColor, UIColor *)
SimImpValue(kSimPickerTitleColor, UIColor *)


@interface SimPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>{
}

@end

@implementation SimPickerView

- (id)initWithTitle:(NSString *)title{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIToolbar *pickerViewToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.width, 44.0)];
        [pickerViewToolBar setBackgroundColor:HEXRGBCOLOR(0xf4f4f4)];
        
        NSMutableArray *items = [NSMutableArray array];
        if (ABOVE_IOS7) {
            UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            fixedItem.width = 10;
            [items addObject:fixedItem];
        }
        
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissToCancel)];
        [items addObject:leftBarButtonItem];
        [leftBarButtonItem setTintColor:SimValue(kSimPickerItemColor, HEXRGBCOLOR(0x64dcdc))];

        
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [items addObject:flexibleSpace];
        if (title.length > 0) {
            UILabel *toolbarTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0, self.width-100, 21.0)];
            toolbarTitleLabel.text = title;
            toolbarTitleLabel.backgroundColor = [UIColor clearColor];
            toolbarTitleLabel.textAlignment = NSTextAlignmentCenter;
            toolbarTitleLabel.font = [UIFont systemFontOfSize:14];
            toolbarTitleLabel.textColor = SimValue(kSimPickerTitleColor, HEXRGBCOLOR(0x666666));
            [toolbarTitleLabel sizeToFit];
            UIBarButtonItem *titleItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarTitleLabel];
            [items addObject:titleItem];
            
            UIBarButtonItem *flexibleSpace2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            [items addObject:flexibleSpace2];
        }
        
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissToFinish)];
        [rightBarButtonItem setTintColor:SimValue(kSimPickerItemColor, HEXRGBCOLOR(0x64dcdc))];
        [items addObject:rightBarButtonItem];
        if (ABOVE_IOS7) {
            UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            fixedItem.width = 10;
            [items addObject:fixedItem];
        }
        
        [pickerViewToolBar setItems:items];
        [self addSubview:pickerViewToolBar];
        
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, pickerViewToolBar.bottom, self.width, 216.0)];
        _pickerView.showsSelectionIndicator = YES;
        _pickerView.backgroundColor = [UIColor clearColor];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        [self addSubview:_pickerView];
        
        self.height = _pickerView.bottom;
    }

    return self;
}


#pragma mark - UIPickerViewDataSource
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (self.widthForCol) {
        return self.widthForCol(component);
    }
    
    return 60;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (_numberOfCol) {
        return _numberOfCol();
    }
    
    return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (_numberOfRowForCol) {
        return _numberOfRowForCol(component);
    }
    
    return 0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *customLabel = (UILabel *)view;
    if (!customLabel) {
        customLabel = [[UILabel alloc] init];
        customLabel.textAlignment = NSTextAlignmentCenter;
        [customLabel setFont:[UIFont systemFontOfSize:18]];
        customLabel.textColor = kSimPickerContentColor;
    }
    
    NSString *content = @"";
    if (_contentForColWithRow) {
        content = _contentForColWithRow(component, row);
    }
    
    customLabel.text = content;
    return customLabel;
}

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [self didSelectRowAndCol];
    
}

#pragma mark - Finish method
- (void)didSelectRowAndCol
{
    if (_selecteBlock) {
        _selecteBlock(self);
    }
}

- (void)dismissToCancel
{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

- (void)dismissToFinish
{
    [self didSelectRowAndCol];
    if (_finishBlock) {
        _finishBlock(self);
    }
}

- (NSArray *)selectedRows
{
    NSMutableArray *rows = [NSMutableArray array];
    NSInteger colCount =  [_pickerView numberOfComponents];
    for (int i = 0; i < colCount; i++) {
        [rows addObject:@([_pickerView selectedRowInComponent:i])];
    }
    
    return rows;
}

- (NSArray *)selectedContents
{
    NSMutableArray *contents = [NSMutableArray array];
    NSArray *selectedRows = [self selectedRows];
    for (int i = 0; i < selectedRows.count; i++) {
        NSString *content = @"";
        if (_contentForColWithRow) {
            content = _contentForColWithRow(i, [selectedRows[i] integerValue]);
        }
        [contents addObject:content];
    }
    
    return contents;
}

- (void)scrollToRows:(NSArray *)rows animated:(BOOL)animated
{
    for (int i = 0; i < rows.count; i++) {
        NSInteger row = [rows[i] integerValue];
        [_pickerView selectRow:row inComponent:i animated:animated];
    }
}



@end
